package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.LecCourseAddDTO;
import com.cheer.param.LecCourseListParam;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.LecCourseService;
import com.cheer.vo.LecCourseInfoVO;
import com.cheer.vo.LecCourseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户信息
 *
 * @author cheer
 */
@RestController
@Api(tags = "讲座")
@RequestMapping("/course")
public class LecCourseController {

    @Autowired
    private LecCourseService lecCourseService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @PostMapping("/list")
    @ApiOperation("列表")
    public Result<Page<LecCourseVO>> list(@RequestBody LecCourseListParam lecCourseListParam) {

        return Result.success(lecCourseService.list(lecCourseListParam));
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public Result delete(@RequestBody @Valid List<Integer> ids) {
        lecCourseService.delete(ids);
        return Result.success();
    }

    @PostMapping("/review")
    @ApiOperation("批阅")
    public Result review(@RequestParam Integer status, @RequestBody @Valid List<Integer> ids) {
        lecCourseService.review(ids, status);
        return Result.success();
    }

    @PostMapping("/add")
    @ApiOperation("添加")
    public Result add(@RequestBody @Valid LecCourseAddDTO addDTO) {
        lecCourseService.add(addDTO);
        return Result.success();
    }

    @GetMapping("/info")
    @ApiOperation("详情")
    public Result<LecCourseInfoVO> info(@RequestParam Integer id) {
        return Result.success(lecCourseService.info(id));
    }

    @PostMapping("/update")
    @ApiOperation("更新")
    public Result update(@RequestBody @Valid LecCourseAddDTO addDTO) {
        lecCourseService.update(addDTO);
        return Result.success();
    }

    @PostMapping("/download")
    @ApiOperation("下载")
    public void download(@RequestBody LecCourseListParam lecCourseListParam) {
        if (lecCourseListParam.getOrg() == null) {
            lecCourseListParam.setOrg(userInfoHolder.getAdmin().getPid());
        }
        lecCourseService.download(lecCourseListParam);
    }
}
